<?php


namespace app\common\model\mysql;


use think\Model;

class AdminAuth extends Model
{
    protected $autoWriteTimestamp = true;

    public function getAttrUsername($username)
    {
        $result = $this->where('username',$username)->find();
        if(empty($result)){
            return false;
        }
        return $result;
    }

    /**
     * @param $id 主键
     * @param $field
     * @param string $value
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateGroup($id, $field, $value = '')
    {
        $updateGroup = $this->find($id);
        $updateGroup->setAttr($field, $value);
        $updateGroup->save();
        return '更新成功';
    }

    //删除用户权限组
    public function delGroup($id)
    {
        if (1 == $id) {
            $result = ['code' => -1, 'msg' => '角色【系统管理员】禁止删除'];
        } else {
            $group = $this->find($id);
            if ($group) {
                $group->delete();
                $result = ['code' => 0, 'msg' => '删除成功'];
            } else {
                $result = ['code' => -1, 'msg' => '删除的角色不存在'];
            }
        }
        return $result;
    }

    public function addSave($id,$username)
    {
        $data = [
            'rules' => 0,
            'username' =>$username,
            'uid' => $id
        ];

        $this->save($data);
    }
}
